﻿
using System;
using System.ComponentModel;
using System.Drawing;
using System.Xml.Serialization;
using Newtonsoft.Json;
using SmartMotion.Core;

namespace CanVasLib.Diagram
{
	public abstract class Entity: BindableBase
	{
		#region Fields
		/// <summary>
		/// 当前实体是否被鼠标悬停
		/// </summary>
		protected internal bool Hovered = false;
		/// <summary>
		/// 实体所属的控件
		/// </summary>
		protected Canvas site;
		/// <summary>
		/// 实体是否被选择
		/// </summary>
		protected bool isSelected = false;

		/// <summary>
		/// 默认字体
		/// </summary>
		protected Font Font = new Font("Verdana", 10F);

		/// <summary>
		/// 默认黑色笔
		/// </summary>
		protected Pen BlackPen = new Pen(Brushes.Black, 1F);

		#endregion	

		#region Properties

		[Browsable(false)]
		public string ObjectId { get; set; } = Guid.NewGuid().ToString();

		/// <summary>
		/// 实体是否被选择 画布使用	
		/// </summary>
		[Browsable(false)]
        [JsonIgnore]
		public bool IsSelected
		{
			get { return isSelected; }
			set { isSelected = value; }
		}
		/// <summary>
		///获取或设置实体所属的画布
		/// </summary>
		[Browsable(false)]
		[JsonIgnore]
		public Canvas Site
		{
			get
			{
				return site;
			}
			set
			{
				site = value;
				SiteChanged();
			}
		}

		#endregion

		#region Constructor
		/// <summary>
		/// Default ctor
		/// </summary>
		public Entity()
		{
		}

		/// <summary>
		/// Ctor with the site of the entity
		/// </summary>
		/// <param name="site"></param>
		public Entity(Canvas site)
		{
			this.site = site;
		}


		#endregion

		#region Methods

		protected virtual void SiteChanged()
		{
		}

		/// <summary>
		/// 在控件上绘制实体
		/// </summary>
		/// <param name="g">the graphics object to paint on</param>
		public abstract void Paint(Graphics g);
		/// <summary>
		/// 测试形状是否被鼠标击中
		/// </summary>
		/// <param name="p"></param>
		/// <returns></returns>
		public abstract bool Hit(Point p);
		/// <summary>
		///使实体无效
		/// </summary>
		public abstract void Invalidate();
		/// <summary>
		/// 在画布上移动实体
		/// </summary>
		/// <param name="p">the shifting vector, not an absolute position!</param>
		public abstract void Move(Point p);

		#endregion
	}
}
